package com.jian.mall.produce.controller;

import java.util.Arrays;
import java.util.List;

import com.jian.mall.produce.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jian.mall.produce.entity.CategoryEntity;
import com.jian.mall.produce.service.CategoryService;
import com.jian.mall.common.utils.R;



/**
 * 商品三级分类
 *
 * @author tainanle
 * @email 12345678@gmail.com
 * @date 2022-08-10 22:48:11
 */
@RestController
@RequestMapping("produce/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     *  查出所有分类以及子分类,以树形结构组装
     */
    @RequestMapping("/list/tree")
    public R list(){
        List<CategoryEntity> list = categoryService.listWithTree();
        return R.ok().put("data", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("produce:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("produce:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("produce:category:update")
    public R update(@RequestBody CategoryEntity[] category){
//		categoryService.updateById(category);
        // 更新完分类信息之后 其他级联了分类信息的数据库也需要修改
        categoryService.updateBatchById(Arrays.asList(category));
        // CategoryBrandRelationController
        Arrays.asList(category).forEach((item)->{
            categoryBrandRelationService.updateByCategoryIdAndName(item.getCatId(),item.getName());
        });
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("produce:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //categoryService.removeByIds(Arrays.asList(catIds));

        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
